/*
 * @Author: yansheng 610363806@qq.com
 * @Date: 2024-06-23 22:30:01
 * @LastEditors: yansheng 610363806@qq.com
 * @LastEditTime: 2024-06-30 01:50:11
 * @FilePath: \cell-admin\src\store\side-menu.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { h } from 'vue'
import { defineStore } from 'pinia'
import config from '@/config/index.js'
import { getPower } from "@/utils/power.js"
const generator = (routerMap) => {
    return routerMap.map((item) => {
        const { title, hideInMenu, icon } = item.meta || {}
        const currentRouter = {
            label: title,
            key: item.path,
            icon: icon ? h('i', { class: icon }) : null

        }

        item.redirect && (currentRouter.redirect = item.redirect)

        if (item.children && item.children.length > 0) {
            currentRouter.children = generator(item.children)
        }

        return hideInMenu ? null : currentRouter
    })
}

const emptyMenu = [{ label: '', key: '' }]

const useSideMenuStore = defineStore('sideMenu', {
    state: () => ({ menus: emptyMenu, menuMap: new Map() }),
    getters: {
        onlyMenu() {
            return config.onlyMenu ?
                this.menus.length > 1 || (this.menus[0] && this.menus[0].children && this.menus[0].children.length) :
                true
        }
    },
    actions: {
        changeSide(side) {
            if (!side) return

            if (!this.menuMap.has(side.path)) {
                this.menuMap.set(side.path, generator(side.children) || [])
            }

            this.menus = this.menuMap.get(side.path)
        }
    }
})

export default useSideMenuStore